/*
  Copyright (c) 2011, Mikheev Rostislav
  Copyright (c) 2011, FreshFarsh team

  Steel Engine source code is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
/**
  @file control.cpp
  @author Mikheev Rostislav <hacenator@gmail.com>
  @date 23.03.2011
  @brief Abstract GUI control class source code
*/

#include "gui/control.h"

namespace _STEEL_NAMESPACE {

GUIControl::GUIControl()
  : _id(0),
  _parent(NULL),
  _name(NULL),
  _on_click(NULL),
  _on_mouse_move_out(NULL),
  _on_mouse_move_over(NULL),
  _on_key_down(NULL),
  _on_key_up(NULL),
  _on_focus_get(NULL),
  _on_focus_lost(NULL),
  _on_mouse_down(NULL),
  _on_mouse_up(NULL),
  _on_mouse_move(NULL),
  _focused(false)
{
    // pass
}

GUIControl::GUIControl(const char * name)
  : _id(0),
  _parent(NULL),
  _name(NULL),
  _on_click(NULL),
  _on_mouse_move_out(NULL),
  _on_mouse_move_over(NULL),
  _on_key_down(NULL),
  _on_key_up(NULL),
  _on_focus_get(NULL),
  _on_focus_lost(NULL),
  _on_mouse_down(NULL),
  _on_mouse_up(NULL),
  _on_mouse_move(NULL),
  _focused(false)
{
  set_name(name);
}

GUIControl::~GUIControl() {
  delete [] _name;
}

bool GUIControl::set_id(GUIControlId id) {
  assert(id);

  // check waht  id no still not seted
  if (_id == 0) {
    _id = id;
    return true;
  } else {
    LOG_WARN("GUIControl::set_id() Id already set and cannot be changed");
  }

  return false;
};

const char * GUIControl::set_name(const char * name) {
    if (name != NULL) {
      delete [] _name;

      int len = strlen(name);
      _name = new char[len + 1];
      strcpy(_name, name);
      //TODO(hacenator): really this is must be max control name size and safe copy
    }
}

void GUIControl::Show() {
  _visible = true;
}

void GUIControl::Hide() {
  _visible = false;
}

void GUIControl::set_width(int value) {
  if (value <= 0) {
    LOG_WARN("GUIControl::set_width() Width must be greater than null");
    return;
  }

  _width = value;
}

void GUIControl::set_height(int value) {
  if (value <= 0) {
    LOG_WARN("GUIControl::set_height() Height must be greater than null");
    return;
  }

  _height = value;
}

Rect GUIControl::GetControlRect() {
  Rect rect;

  rect.left = _x;
  rect.top = _y;
  rect.right = _x + _width;
  rect.bottom = _y + _height;

  return rect;
}

void GUIControl::Click(const GUIControl * sender) {
  LOG_INFO("vUIControl(%s)->Click", _name);

  if (_on_click != NULL) _on_click(sender);
};

void GUIControl::MoveOver(const GUIControl * sender, int x, int y) {
  LOG_INFO("vUIControl(%s)->MoveOver", _name);

  if (_on_mouse_move_over != NULL) _on_mouse_move_over(sender, x, y);
};

void GUIControl::MoveOut(const GUIControl * sender) {
  LOG_INFO("vUIControl(%s)->MoveOut", _name);

  if (_on_mouse_move_out != NULL) _on_mouse_move_out(sender);
};

void GUIControl::KeyUp(const GUIControl * sender, int key) {
  LOG_INFO("vUIControl(%s)->KeyUp", _name);

  if (_on_key_up != NULL) _on_key_up(sender, key);
};

void GUIControl::KeyDown(const GUIControl * sender, int key) {
  LOG_INFO("vUIControl(%s)->KeyDown", _name);

  if (_on_key_down != NULL) _on_key_down(sender, key);
};

void GUIControl::FocusGet(const GUIControl * sender) {
  LOG_INFO("vUIControl(%s)->FocusGet", _name);
  _focused = true;

  if (_on_focus_get != NULL) _on_focus_get(sender);
};

void GUIControl::FocusLost(const GUIControl * sender) {
  LOG_INFO("vUIControl(%s)->FocusLost", _name);
  _focused = false;

  if (_on_focus_lost != NULL) _on_focus_lost(sender);
};

void GUIControl::MouseDown(const GUIControl * sender, int x, int y) {
  LOG_INFO("vUIControl(%s)->MouseDown", _name);

  if (_on_mouse_down != NULL) _on_mouse_down(sender, x, y);
};

void GUIControl::MouseUp(const GUIControl * sender, int x, int y) {
  LOG_INFO("vUIControl(%s)->MouseUp", _name);

  if (_on_mouse_up != NULL) _on_mouse_up(sender, x, y);
};

void GUIControl::MouseMove(const GUIControl * sender, int x, int y) {
  LOG_INFO("vUIControl(%s)->MouseMove", _name);

  if (_on_mouse_move != NULL) _on_mouse_move(sender, x, y);
};

}
